package com.ipfsemc.ipfsemc.service.imp;

import com.ipfsemc.ipfsemc.mapper.FilMapper;
import com.ipfsemc.ipfsemc.mapper.UserInfoMapper;
import com.ipfsemc.ipfsemc.model.Fil;
import com.ipfsemc.ipfsemc.model.UserInfo;
import com.ipfsemc.ipfsemc.service.IFilService;
import com.ipfsemc.ipfsemc.service.IUserInfoService;
import com.ipfsemc.ipfsemc.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    RedisTemplate<String, Object> rts;

    @Autowired
    private FilMapper filMapper;


    @Override
    public UserInfo selectByPrimaryKey(String userId) {

        return userInfoMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int addUser(UserInfo userInfo) {
        return userInfoMapper.insertSelective(userInfo);
    }

    @Override
    public int updateUser(UserInfo userInfo) {
        return userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public ResponseBase send(String phone) {
        Response jsonData = new Response();

//        long timeout = 5;
        String code = CodecUtil.createRandomNum(6);
//        Integer resultCode = SendMessageUtil.send("helloRen","d41d8cd98f00b204e980",phone,"尊敬的用户，您好，您的验证码为："+code+"，请于"+timeout+"分钟内正确输入，如非本人操作，请忽略此短信。");
        Integer resultCode = 1;
        if (resultCode > 0) {

            //将手机号和验证码存入redis,生存时间为5分钟
            rts.opsForValue().set(phone + "_yzm", code, 5, TimeUnit.MINUTES);
            //上面是简化版

//            ValueOperations forValue = rts.opsForValue();
//            forValue.set(phone + "_yzm", code);
//            rts.expire(phone + "_yzm", 5 * 60 * 1000, TimeUnit.MILLISECONDS);


            jsonData.setMessage("已发验证码" + code + "送至：" + phone);
            jsonData.setCode(0);
            return jsonData;
        } else {
            jsonData.setCode(-1);
            jsonData.setMessage(SendMessageUtil.getMessage(resultCode));
        }


        return jsonData;
    }

    @Override
    public int updatePhone(String phone,String code) {

        //手机验证码发送成功，业务处理
        UserInfo userInfo=new UserInfo();
        userInfo.setUserId(phone);
        userInfo.setPhone(code);
        userInfoMapper.updateByPrimaryKeySelective(userInfo);

        return 0;
    }


    /**
     * 根据手机号查找用户  主要是是查看是否报备
     *
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo userByPhone(String phone) {
        return userInfoMapper.userByPhone(phone);
    }

    @Override
    public List<UserInfo> list(UserInfo userInfo) {

        return userInfoMapper.list(userInfo);
    }

    @Override
    public UserInfo selectBounty(UserInfo userInfo) {
        return userInfoMapper.selectBounty(userInfo);
    }

    @Override
    public UserInfo selectOne(String userId) {
        return userInfoMapper.selectOne(userId);
    }

    @Override
    public int updateFil(UserInfo userInfo) {
        UserInfo user = userInfoMapper.userByPhone(userInfo.getPhone());//转fil的人

        if(null==user){
            //此人不存在
            return -2;
        }

        //判断账户fil是否大于要装的fil
        if (user.getFil().compareTo(userInfo.getFil()) != -1) {

            BigDecimal userFil = user.getFil().subtract(userInfo.getFil());
            user.setFil(userFil);
            int x = userInfoMapper.updateFil(user);

            Fil fil = new Fil();
            fil.setFilType(2);
            fil.setUserId(user.getUserId());
            fil.setFilEvent("转出 用户：" + user.getUserId() + " 电话：" + userInfo.getPhone());
            fil.setFilItem("-" + userInfo.getFil() + "\r\nFil\r\n（总FIL：" + userFil + "）");
            filMapper.insertSelective(fil);


            //被转fil的人
            UserInfo toUser = userInfoMapper.userByPhone(userInfo.getToPhone());
            BigDecimal toUserFil = toUser.getFil().add(userInfo.getFil());

            //修改被转fil币账户的人
            toUser.setFil(toUserFil);
            int y = userInfoMapper.updateFil(toUser);

            //添加fil明细记录
            Fil toFil = new Fil();
            toFil.setUserId(toUser.getUserId());
            toFil.setFilEvent("转入 用户：" + toUser.getUserId() + " 电话：" + userInfo.getToPhone());
            toFil.setFilItem("+" + userInfo.getFil() + "\r\nFil\r\n（总FIL：" + toUserFil + "）");
            filMapper.insertSelective(toFil);


        } else {
            //fil不足
            return -1;
        }

        return 1;
    }

    @Override
    public UserInfo millItem(UserInfo userInfo) {
        return userInfoMapper.millItem(userInfo);
    }


}
